package com.cskaoyan.wx.controller;

import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.service.SearchService;
import com.cskaoyan.wx.vo.SearchGoodsVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author LiaoLong
 * @date 2021-05-12 22:47
 */
@RestController
@RequestMapping("wx/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("index")
    public BaseRespVo index(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getSession().getAttribute("username");
        SearchGoodsVO searchGoodsVO = searchService.getSearchs(username);
        return BaseRespVo.ok(searchGoodsVO);
    }

    @GetMapping("helper")
    public BaseRespVo helper(String keyword){
        List<String> keywordList = searchService.fuzzySearchKeywords(keyword);
        return BaseRespVo.ok(keywordList);
    }

    @PostMapping("clearhistory")
    public BaseRespVo clearhistory(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getSession().getAttribute("username");
        searchService.deleteSearchHistory(username);
        return BaseRespVo.ok();
    }
}
